package com.richapp.launcher.ui.fragments.home.app;

import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

import com.richapp.launcher.R;
import com.richapp.launcher.application.AppInfo;
import com.richapp.launcher.ui.adapter.AppListAdapter;
import com.richapp.launcher.ui.view.TabPageView;
import com.richapp.launcher.utils.AppManager;
import com.richapp.launcher.utils.Toolkit;

public class SmartAppFragment extends Fragment {
	private TabPageView mTabPageView;
	List<AppInfo> mApplications = null;
	private GridView mAppGrid;
	private AppListAdapter mAppAdapter = null;
	private boolean mUninsallRecomm = false;
	private int mUninsallPostion;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.smart_app, container, false);
		init(view);
		return view;
	}

	private void init(View view) {
		mAppGrid = (GridView) view.findViewById(R.id.app_grid);
		mAppGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				AppInfo app = mAppAdapter.getItem(position);
				try {
					startActivity(app.intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		mAppGrid.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				AppInfo app = mAppAdapter.getItem(position);
				ComponentName component = app.intent.getComponent();
				if (component != null) {
					String packageName = component.getPackageName();
					if (!Toolkit.isEmpty(packageName)) {
						mUninsallRecomm = false;
						mUninsallPostion = position;
						uninstall(app.title.toString(), packageName);
					}
				}
				return true;
			}
		});
		mApplications = loadApplications();
		mAppAdapter = new AppListAdapter(getActivity(), R.layout.app_grid_item,
				mApplications);
		mAppGrid.setAdapter(mAppAdapter);
	}

	OnKeyListener mOnKey = new OnKeyListener() {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (v == mAppGrid && event.getAction() == KeyEvent.ACTION_DOWN
					&& keyCode == KeyEvent.KEYCODE_DPAD_UP) {
				if (mTabPageView != null
						&& mTabPageView.getCurrTabView() != null) {
					mTabPageView.getCurrTabView().requestFocus();
				}
			}
			return false;
		}
	};

	@Override
	public void onAttach(Activity activity) {
		mTabPageView = (TabPageView) activity.findViewById(R.id.tabPageView);
		super.onAttach(activity);
	}

	/**
	 * 卸载应用
	 */
	private void uninstall(final String appName, final String packageName) {
	}

	private void refreshData() {
		mApplications = loadApplications();
		mAppAdapter = new AppListAdapter(getActivity(), R.layout.app_grid_item,mApplications);
		mAppGrid.setAdapter(mAppAdapter);
		int size = mApplications.size();
		if (!mUninsallRecomm) {
			mAppGrid.requestFocus();
			if (size > 0) {
				if (mUninsallPostion > size - 1) {
					mAppGrid.setSelection(size - 1);
				} else {
					mAppGrid.setSelection(mUninsallPostion);
				}
			}
		}
	}

	/**
	 * 加载系统所有应用程序
	 */
	private List<AppInfo> loadApplications() {
		List<AppInfo> apps = AppManager.findAllApps(getActivity());
		if (!Toolkit.isEmpty(apps)) {
			return apps;
		}
		return Collections.emptyList();
	}

	public static SmartAppFragment getInstance() {
		return new SmartAppFragment();
	}

	/**
	 * 接收应用更新广播
	 * 
	 * @author penglin
	 * @version HDMNV100R001, 2013-6-24
	 */
	class AppRecommendReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			// if (AppRecommendService.RECOMMEND_ACTION.equals(action)) {
			// updateAppRecommGrid();
			// } else
			if (Intent.ACTION_PACKAGE_CHANGED.equals(action)
					|| Intent.ACTION_PACKAGE_REMOVED.equals(action)
					|| Intent.ACTION_PACKAGE_ADDED.equals(action)) {
				refreshData();
			}
		}
	}
}
